package wh.coach._lambda;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author hangwen
 * @date 2023/7/1
 */
public class Main {

    public static void main(String[] args) {
        //final int i=0;
        final AtomicInteger counter = new AtomicInteger();

        //写法1，具体类
        int result1 = callCalculate(new AddOne());
        System.out.println("call AddOne instance,result:" + result1);

        //写法2，匿名类
        int result2 = callCalculate(new Calculate() {
            @Override
            public int cal(final int val) {
                //i =2 //错误 i是final不可变更
                counter.getAndIncrement();

                return val * 2;
            }

        });
        System.out.println("call anonymous class instance,result:" + result2);

        //写法3，lambda
        int result3 = callCalculate(j -> j * 10);
        System.out.println("call lambda,result:" + result3);

        callCalculate2((数值1, v2) -> 数值1 + v2);

    }

    private static int callCalculate(final Calculate calculate) {
        int i = 1;

        return calculate.cal(i);
    }

    private static int callCalculate2(final Calculate2 calculate) {
        return calculate.cal(1, 2);
    }

    private static class AddOne implements Calculate {

        @Override
        public int cal(final int val) {
            return val + 1;
        }

    }
}
